package com.sanyang.pda.presenter;

import com.google.gson.Gson;
import com.sanyang.pda.bean.ByBarcodeBean;
import com.sanyang.pda.bean.TakeStockBean;
import com.sanyang.pda.bean.TakeStockListBean;
import com.sanyang.pda.contract.ConfirmStockContract;
import com.sanyang.pda.net.NetUtils;
import com.wangshen.base.net.bean.BaseAppEntity;
import com.wangshen.base.ui.mvp.base.presenter.BasePresenter;

import java.util.List;

import io.reactivex.functions.Consumer;
import okhttp3.MediaType;
import okhttp3.RequestBody;

public class ConfirmStockPresenter extends BasePresenter<ConfirmStockContract.View> implements  ConfirmStockContract.Presenter{

    @Override
    public void stockAdd(TakeStockBean takeStockBean) {
        RequestBody requestBody = RequestBody.create(MediaType.parse("\"application/json;charset=UTF-8\""), new Gson().toJson(takeStockBean));
        NetUtils.geApi().confirmOrAddCommodityInfo(requestBody)
                .compose(this.handleEverythingResult())
                .subscribe(new Consumer<BaseAppEntity<TakeStockListBean>>() {
                    @Override
                    public void accept(BaseAppEntity<TakeStockListBean> takeStockListBeanBaseAppEntity) throws Exception {
                            getView().showMessage(takeStockListBeanBaseAppEntity.getMsg());
                            getView().finishActivity();
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        getView().showMessage(throwable.getMessage());
                    }
                });
    }
    @Override
    public void selectArticleInfoByBarcode(String barcode,String number) {
        NetUtils.geApi().selectArticleInfoByBarCodeAndNo(barcode,number)
                .compose(this.handleEverythingResult())
                .subscribe(new Consumer<BaseAppEntity<List<ByBarcodeBean>>>() {
                    @Override
                    public void accept(BaseAppEntity<List<ByBarcodeBean>> listBaseAppEntity) throws Exception {
                        getView().setGoodsList(listBaseAppEntity.getData());
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        getView().showMessage(throwable.getMessage());
                    }
                });
    }

}
